package roslan.musicbox.client.web;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import roslan.musicbox.client.data.ClientMusicTag;
import roslan.musicbox.exception.TagElementException;
   
public class WebClient {  
	private static String SERVICE_HOST = "localhost";  
	private static String SERVICE_URL = 
		"http://xxx:8080/RoslanMusicboxService/";  
    private static String GET_MUSICTAG_SERVICE = SERVICE_URL + "getMusicTag?";
    
    public static void setHost(String filename){
    	Scanner scanner = null;
    	try{
    		scanner = new Scanner(new File(filename));
    	}catch(Exception e){}
    	String host = scanner.nextLine();
    	host = host.substring(host.indexOf("=")+1);
    	host = host.trim();
    	
    	String url = scanner.nextLine();
    	url = url.substring(url.indexOf("=")+1);
    	url = url.trim();
    	
    	scanner.close();
    	
    	SERVICE_HOST = host;
    	SERVICE_URL = url;
    }
         
    public InputStream getSoapInputStream(String url){  
    	System.out.println(SERVICE_HOST+" "+SERVICE_URL);
    	InputStream inputStream = null;  
        try{  
        	URL urlObj = new URL(url);
            HttpURLConnection urlConn = (HttpURLConnection)urlObj.openConnection();  
            urlConn.setRequestProperty("Host", SERVICE_HOST);
            urlConn.connect();
            inputStream = urlConn.getInputStream();  
        }catch(MalformedURLException e){  
            e.printStackTrace();  
        }catch(IOException e){  
            e.printStackTrace();  
        }  
        return inputStream;  
    }  
       
    public Vector<ClientMusicTag> getMusicTagResult(
    	 String title, String album, String artist,
    	 String year,  String trackNo, String filename){
    	Document document;  
        DocumentBuilderFactory documentBF = DocumentBuilderFactory.newInstance();  
        documentBF.setNamespaceAware(true);
    	Vector<ClientMusicTag> musicTagResult = 
    			new Vector<ClientMusicTag>();
    	musicTagResult.clear();
        try{ 
            DocumentBuilder documentB = documentBF.newDocumentBuilder();
            String query = GET_MUSICTAG_SERVICE + 
            			"title="    + URLEncoder.encode(title,"UTF-8")
            		 + "&album="    + URLEncoder.encode(album,"UTF-8")
             		 + "&artist="   + URLEncoder.encode(artist,"UTF-8")
             		 + "&year="     + URLEncoder.encode(year,"UTF-8")
             		 + "&trackNo="  + URLEncoder.encode(trackNo,"UTF-8")
             		 + "&filename=" + URLEncoder.encode(filename,"UTF-8");
            InputStream inputStream = getSoapInputStream(query);  
            document = documentB.parse(inputStream);  
            NodeList nl = document.getElementsByTagName("MusicTag");  
            int len = nl.getLength();  
            for(int i = 0; i < len; i++){  
               Node n = nl.item(i);
               ClientMusicTag tag = ClientMusicTag.
                 	getTagFromElement((Element)n); 
               tag.setApic(ClientMusicTag.defaultPic);
               musicTagResult.add(tag);
            }
            inputStream.close();  
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (DOMException e){  
            e.printStackTrace();  
        }catch (ParserConfigurationException e){  
            e.printStackTrace();  
        }catch(SAXException e){  
            e.printStackTrace();  
        }catch (IOException e){  
            e.printStackTrace();
        }catch (TagElementException e) {
        	e.printStackTrace();
        }
    	return musicTagResult;
	}         
}